﻿Imports System.IO
Imports System.Text
Imports System.Text.Encoding
Imports System.IO.FileStream

Module BasicFileSystem

'文件名结构
    Public Structure FileNameInfo
        Public Folder As String     '文件夹
        Public SigleName As String  '文件名+扩展名
        Public ONlyName As String  '仅文件名
        Public ExtensionName As String  '扩展名
    End Structure

    '读取文件(文件名)
    Public Function ReadTextFile(ByVal FileFullName As String)
        Dim File As New StreamReader(FileFullName, Encoding.Default)
        Dim FileText = File.ReadToEnd()
        File.Close()
        Return FileText
    End Function

    '判断文件是否存在(文件名)
    Public Function IsFileExsts(ByVal FileFullName As String) As Boolean
        IsFileExsts = My.Computer.FileSystem.FileExists(FileFullName)
    End Function

    '判断文件夹是否存在(文件夹)
    Public Function IsDirectoryExists(ByVal DirectoryFullName As String) As Boolean
        IsDirectoryExists = My.Computer.FileSystem.DirectoryExists(DirectoryFullName)
    End Function

    '获取文件所在文件夹(文件名)
    Public Function GetParentFolder(ByVal FileFullName As String) As String
        If IsFileExsts(FileFullName) = True Then
            GetParentFolder = My.Computer.FileSystem.GetFileInfo(FileFullName).DirectoryName
        Else
            Dim i As Integer
            Dim OnlyS As String
            OnlyS = FileFullName
            i = InStrRev(OnlyS, "\")
            OnlyS = Strings.Left(OnlyS, i - 1)
            GetParentFolder = OnlyS
        End If
    End Function

    '从全名中取出文件名,含扩展名(文件名)
    Public Function GetSingleName(ByVal FileFullName As String) As String
        If IsFileExsts(FileFullName) = True Then
            GetSingleName = My.Computer.FileSystem.GetName(FileFullName)
        Else
            Dim p As Short
            p = InStrRev(FileFullName, "\")
            If p > 0 Then
                GetSingleName = Strings.Right(FileFullName, Strings.Len(FileFullName) - p)
            Else
                GetSingleName = ""
            End If
        End If

    End Function

    '从全名中取出文件名简称,去除路径及扩展名(文件名)
    Public Function GetOnlyname(ByVal FileFullName As String) As String
        If IsFileExsts(FileFullName) = True Then
            Dim FileName As String
            Dim FileExtension As String

            FileName = My.Computer.FileSystem.GetName(FileFullName)
            FileExtension = My.Computer.FileSystem.GetFileInfo(FileFullName).Extension
            GetOnlyname = Microsoft.VisualBasic.Strings.Replace(FileName, FileExtension, "")
        Else
            Dim i As Integer
            Dim OnlyS As String
            OnlyS = FileFullName
            i = InStrRev(OnlyS, "\")
            OnlyS = Strings.Right(OnlyS, Len(OnlyS) - i)
            i = InStrRev(OnlyS, ".")
            OnlyS = Strings.Left(OnlyS, i - 1)
            GetOnlyname = OnlyS
        End If
    End Function

    '大写扩展名(文件名)
    Public Function GetFileExtension(ByVal FileFullName As String) As String
        If IsFileExsts(FileFullName) = True Then
            GetFileExtension = UCase(My.Computer.FileSystem.GetFileInfo(FileFullName).Extension)
        Else
            Dim i As Integer
            Dim OnlyS As String
            OnlyS = FileFullName
            i = InStrRev(OnlyS, ".")
            OnlyS = Strings.Right(OnlyS, Len(OnlyS) - i + 1)
            GetFileExtension = UCase(OnlyS)
        End If
    End Function

    '获取filenameinfo结构(文件名)
    Public Function GetFileNameInfo(ByVal FileFullName As String) As FileNameInfo
        Dim FNI As FileNameInfo = Nothing
        If IsFileExsts(FileFullName) = True Then
            FNI.Folder = My.Computer.FileSystem.GetFileInfo(FileFullName).DirectoryName
            FNI.SigleName = My.Computer.FileSystem.GetName(FileFullName)
            FNI.ExtensionName = UCase(My.Computer.FileSystem.GetFileInfo(FileFullName).Extension)
            FNI.ONlyName = Microsoft.VisualBasic.Strings.Replace(FNI.SigleName, FNI.ExtensionName, "")
        Else
            Dim i As Integer
            FNI.Folder = FileFullName
            i = InStrRev(FNI.Folder, "\")
            FNI.Folder = Strings.Left(FNI.Folder, i - 1)
            FNI.SigleName = Strings.Right(FileFullName, Strings.Len(FileFullName) - 1)

            i = InStr(FNI.SigleName, ".")
            FNI.ONlyName = Left(FNI.SigleName, i - 1)
            FNI.ExtensionName = UCase(Strings.Right(FNI.SigleName, Strings.Len(FNI.SigleName) - 1))
        End If
        Return FNI
    End Function

    '重命名(旧文件名,新文件名)
    Public Function ReFileName(ByVal OldFileFullName As String, ByVal NewFileFullName As String) As Boolean
        If IsFileExsts(NewFileFullName) = False And IsFileExsts(OldFileFullName) = True Then
            My.Computer.FileSystem.RenameFile(OldFileFullName, GetSingleName(NewFileFullName))
            ReFileName = IsFileExsts(NewFileFullName)
        Else
            ReFileName = False
        End If
    End Function

    '判断2个文件是否在同一文件夹(第一个文件名,第二个文件名)
    Public Function IsInSameFolder(ByVal OneFile As String, ByVal TowFile As String) As Boolean
        Dim OneFileFolder As String
        Dim TowFileForlder As String
        OneFileFolder = GetParentFolder(OneFile)
        TowFileForlder = GetParentFolder(TowFile)
        If OneFileFolder = TowFileForlder Then
            IsInSameFolder = True
        Else
            IsInSameFolder = False
        End If
    End Function


    '删除一个文件(文件名,是否删除到回收站选项)
    Public Function DelFile(ByVal FileFullName As String, ByVal RecycleOption As FileIO.RecycleOption) As Boolean
        If IsFileExsts(FileFullName) Then
            My.Computer.FileSystem.DeleteFile(FileFullName, FileIO.UIOption.OnlyErrorDialogs, RecycleOption, FileIO.UICancelOption.DoNothing)
        End If
        DelFile = IsFileExsts(FileFullName) Xor True
    End Function

    '仅删除一个文件夹中的文件(文件夹)
    Public Function DelOnlyFileInForlder(ByVal Folder As String) As Boolean
        If My.Computer.FileSystem.DirectoryExists(Folder) Then
            For Each f As String In My.Computer.FileSystem.GetFiles(Folder)
                DelFile(f, FileIO.RecycleOption.SendToRecycleBin)
            Next
        End If
    End Function

    '删除文件夹(文件夹)
    Public Function DelFolder(ByVal Folder As String) As Boolean
        If My.Computer.FileSystem.DirectoryExists(Folder) Then
            My.Computer.FileSystem.DeleteDirectory(Folder, FileIO.DeleteDirectoryOption.DeleteAllContents, FileIO.RecycleOption.SendToRecycleBin)
            DelFolder = IsDirectoryExists(Folder)
        Else
            DelFolder = False
        End If
    End Function

    '移动文件(源文件名,目标文件名)
    Public Function ReMoveFile(ByVal SourceFileName As String, ByVal DestinationFileName As String)
        Dim DestinationFolder As String
        DestinationFolder = GetParentFolder(DestinationFileName)
        If IsDirectoryExists(DestinationFolder) = False Then
            My.Computer.FileSystem.CreateDirectory(DestinationFolder)
        End If
        My.Computer.FileSystem.MoveFile(SourceFileName, DestinationFileName, True)
        Return IsFileExsts(DestinationFileName)
    End Function

    '移动文件到文件夹(源文件名,目标文件夹)
    Public Function ReMoveFileToFolder(ByVal SourceFileName As String, ByVal DestinationFolder As String)
        Dim DestinationFileName As String
        If IsDirectoryExists(DestinationFolder) = False Then
            My.Computer.FileSystem.CreateDirectory(DestinationFolder)
        End If

        DestinationFileName = DestinationFolder & "\" & GetSingleName(SourceFileName)
        My.Computer.FileSystem.MoveFile(SourceFileName, DestinationFileName, True)

        Return IsFileExsts(DestinationFileName)
    End Function

End Module
